<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
use DB;
use Crypt;

class ResetController extends Controller
{
    public function forget()
    {
    	return view('admin.reset.forget');
    }

    public  function sendemail(Request $request)
    {
    	//发送一条简单的消息
		  //   	Mail::raw('这是垃圾邮件,看到就删了吧', function ($message) {
		  //   		$message -> subject('找回密码');//主题
		  // 			$message ->to('237132868@qq.com');//发送到
				// });
		//获取网站邮箱类型
		$mail = $request ->email;
		$com = trim(strrchr($mail,'@'),'@');
		
		$email = 'mail.'.$com;
		
		
		$res = DB::table('users') -> where('email',$request->email) ->first();
		if(!$res)
		{
			return back()->with(['info'=> '您输入的邮箱不存在']);
		}
		 $s = Mail::send('emails.links', ['user' =>$res], function ($message)use($res){
            
            $message->to($res ->email)->subject('找回密码');
        });

		 if($s)
		 {
		 	return view('admin.reset.success',['email' =>$email]);
		 }

    }
    public function inputpass($id,$token)
    {
    	//查询id和token是否一致
    	$t = DB::table('users') ->where('id' , $id) ->first() ->token;
    	if($t !=$token)
    	{
    		return redirect('admin.reset.info') ->with(['info'=>'未知来源']);
    	}

    	return view('admin.reset.newpass',['info' =>'请输入新密码','id' =>$id]);
    }

    public function updatepass(Request $request)
    {
    	if($request->password != $request ->newpassword)
    	{
    		return back() ->with(['info' => '两次密码不一致']);
    	}
    	// dd($request);
    	// dd($request->id);
    	
    	
    	
    	$res = DB::table('users') ->where('id',$request->id) ->update(['password' =>Crypt::encrypt($request ->password)]);
    	// dd($res);
    	
    	if($res)
    	{
    		return redirect('/admin/login') ->with(['info' => '密码修改成功,请登录']);
    	}else
    	{
    		return redirect('/admin/reset/info')->with(['info' => '密码修改失败,请重新修改']);
    	}


    }

    public function info()
    {
    	return view('admin.reset.info');
    }

    public function success()
    {
    	return view('admin.reset.success');
    }
}
